package com.tocean.app.dto.response;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 功能描述：
 *
 * @Author 梁展鹏
 * @Date 2020/5/14 23:58
 */
@ApiModel(value = "BaseResponse", description = "返回协议")
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
@Accessors(chain = true)
public class BaseResponse<T> implements Serializable {

    private static final long serialVersionUID = -4147625560055281066L;

    @ApiModelProperty(value = "请求序列", notes = "客户端生成的一个序列，用于标识本次请求，通常是客户端的时间序列", required = false, hidden = false)
    private String seq;

    @ApiModelProperty(value = "主要业务数据体", notes = "主要业务数据体", required = false, hidden = false)
    private T data;

    private Integer code = 0;

    private String msg = "^_^";

    BaseResponse() {
        this.code = 200;
    }

    BaseResponse(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static <T> BaseResponse<T> success() {
        return new BaseResponse<T>();
    }

    public static <T> BaseResponse<T> success(String msg, T data) {
        BaseResponse<T> response = new BaseResponse<T>();
        response.setMsg(msg).setData(data);
        return response;
    }

    public static BaseResponse success(String msg) {
        BaseResponse response = new BaseResponse();
        response.setMsg(msg);
        return response;
    }

    public static <T> BaseResponse<T> success(T data) {
        BaseResponse<T> response = new BaseResponse<T>();
        response.setData(data);
        return response;
    }

    public static <T> BaseResponse<T> failure(int code) {
        return failure(code, "-_-", null);
    }

    public static <T> BaseResponse<T> failure(int code, String msg) {
        return failure(code, msg, null);
    }

    public static <T> BaseResponse<T> failure(int code, String msg, String[] msgPlaceholder) {
//        Locale locale = LocaleContextHolder.getLocale();
//        ResourceBundle resourceBundle = ResourceBundle.getBundle("i18n/messages", locale);
//        msg = resourceBundle.getString(msg); // 资源文件定义:103014=订单号为{0}的订单已经支付,此时msg相当于103014
//        if (msgPlaceholder != null) {
//            MessageFormat messageFormat = new MessageFormat(msg);
//            msg = messageFormat.format(msgPlaceholder);
//        }
        return new BaseResponse<T>(code, msg);
    }

}
